'use strict';

const Controller = require('egg').Controller;

class CommentController extends Controller {
  async save() {
    let { ctx } = this
    const user_id = ctx.authUser.id
    ctx.validate({
      content: { type: 'string', required: true, desc: '评论内容' },
      video_id: { type: 'int', required: true, desc: '作品id' },
      reply_id: { type: 'int',  desc: '回复id' },
      reply_user_id: { type: 'int',  desc: '回复用户id' }
    })
    const { content, video_id, reply_id, reply_user_id } = ctx.request.body
    if (reply_id > 0) {
      if (reply_user_id === 0) {
        return ctx.apiFail('回复用户id参数错误')
      }
      let c = await ctx.model.Comment.findOne({
        where: {
          id: reply_id
        }
      })
      if (!c) {
        return ctx.apiFail('被回复的评论不存在')
      }
    }
    let video = await ctx.model.Video.findOne({
      where: {
        id: video_id
      }
    })
    if (!video) {
      return ctx.apiFail('视频不存在')
    }
    let res = await ctx.model.Comment.create({
      content,
      video_id,
      user_id,
      reply_id,
      reply_user_id
    })
    ctx.apiSuccess(res)
  }

 
}

module.exports = CommentController;
